// pages/user_center/user_info/user_info.js
const app = getApp()
var common = require("../../../utils/common.js")
var http = require("../../../utils/httpUtils.js")
Page({
  /**
   * 页面的初始数据
   */
  pickSex: function (e) {
    this.setData({
      'dataInfo.u_sex': e.detail.value
    });
  },
  data: {
    columns: ["男", "女", "保密"],
    dateTimeArray1: null,
    dateTime1: common.getTime(new Date() / 1000),
    startYear: null,
    endYear: null,
    dataInfo: {},
    userimg: ''
  },
  // 选择图片
  selectImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: ['album'],
      success: function (res) {
        console.log(res)
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          // url: 'http://127.0.0.1:3200/common/wxUpload', //仅为本地示例，非真实的接口地址
          url: app.globalData.SERVER_IP + '/common/wxUpload', 
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'user': 'test'
          },
          success (res){
            console.log(res)
            console.log(res.data)
            console.log(JSON.parse(res.data).result)
            that.setData({
              userimg : JSON.parse(res.data).result
            })
          }
        })
      }
    })
  },
  formSubmit: function (e) {
    var urls = app.globalData.SERVER_IP + "/user/update_user"
    var parsams = e.detail.value
    parsams.u_id = wx.getStorageSync('userInfo').userid
    parsams.u_birthday = parseInt(new Date(this.data.dateTime1) / 1000)
    parsams.u_header_url = this.data.userimg

    console.log(parsams)

    http.P(urls, parsams, (res) => {
      console.log(res.result.success)
      if (res.result.success) {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        })
      }

    }, (err) => {
      console.log("获取信息失败")
      console.log(err)
    })
  },
  getData: function () {
    var that = this
    // 获取数据
    wx.request({
      url: app.globalData.SERVER_IP + '/user/search_user/u_id/' + wx.getStorageSync('userInfo').userid,
      method: 'GET',
      data: '',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log("获取登录信息", res.data.result)
        that.setData({
          dataInfo: res.data.result.userInfo[0],
          dateTime1: common.getTime(res.data.result.userInfo[0].u_birthday),
          userimg : res.data.result.userInfo[0].u_header_url
        })
      }
    })

  },
  // 选择日期时间
  changeDateTime1(e) {
    this.setData({
      dateTime1: e.detail.value
    });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1,
      dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '个人信息',
    })
    var dateTimePicker = require('../../../utils/dateTimePicker.js');
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    this.setData({
      dateTimeArray1: obj1.dateTimeArray,
      // dateTime1: obj1.dateTime
    });

    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})